const axios = require('axios');
const appid = 'wxce0e6da6bfb0e3bf';
const secret = '6ea142ea17b0b32f7a189e758f77365a';

function main(code) {
  return new Promise((resolve, reject) => {
    const url = `https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${code}&grant_type=authorization_code`;
    axios.get(url).then(res => {
      resolve(res.data.openid);
    }).catch(err => {
      reject(JSON.stringify(err));
    })
  })
}

module.exports = main;